
package com.scalar.chiptrack.sales.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.dao.SalesDAO;
import com.scalar.chiptrack.sales.forms.OrdersForm;
import com.scalar.chiptrack.users.User;


/**
 * Implementation of <strong>Action</strong> that gives customers list,
 * search with customer name, create sales order
 *
 * @author Kesava Kumar Achanala
 * @version $Revision: 1.1.1.1 $ $Date: 2006/12/11 23:43:17 $
 */

public final class OnlineOrdersAction extends ChipTrackAction {


    // ----------------------------------------------------- Instance Variables
        ArrayList ordersList  = new ArrayList();

        private static final String ACTION_NAME = "/onlineOrders.do" ;  // Added by Basha (08/08/2003)

    // --------------------------------------------------------- Public Methods


    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception Exception if business logic throws an exception
     */
    public ActionForward execute(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws Exception {

		// Extract attributes and parameters we will need
		OrdersForm subform = (OrdersForm) form;
		String action = request.getParameter("action");
		
		// Validate the request parameters specified by the user
		HashMap errors = new HashMap();
		
                //Get the session, to retrieve the values
                HttpSession session = request.getSession();
                User user = (User)session.getAttribute (SessionKeys.USER_KEY);
                // Added by Basha (08/08/2003)
                if(user == null)
                {
                    session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
                    return (mapping.findForward ("login"));
                }
                // Added by Basha (08/08/2003)


                if(action == null)
                    request.setAttribute("showList", "FALSE"); //Not to show the list by default
                
                
                if("SearchOrder".equals(action)) {
                    String marketingPartNo = request.getParameter("marketingPartNo");
                        
                    //here salesOrderId is cusomterPo number...
                    String salesOrderId = request.getParameter("salesOrderId");
                    String customerId = request.getParameter("customerId");
                      //  System.out.println("SalesOrderID = " + salesOrderId + " , customerId = " + customerId + " , partno = " + marketingPartNo);
                    if((salesOrderId != null) && (salesOrderId.trim().length() != 0)) 
                        ordersList = SalesDAO.loadOnlineOrdersWithPurchaseOrder(salesOrderId,0,false);                                                            
                    else 
                        ordersList = SalesDAO.loadAllOnlineOrders(Integer.parseInt(customerId),marketingPartNo,false);                        
                       
                    //Show the pendingList only when it searched
                    request.setAttribute("showList", "TRUE");    
                                
		} else if("CustomerOrders".equals(action)) {
                    String marketingPartNo = request.getParameter("marketingPartNo");
                    
                    String salesOrderId = (String)BeanUtils.getSimpleProperty(form,"salesOrderId");
                    int customerId = user.getCustomerId();
                      //  System.out.println("SalesOrderID = " + salesOrderId + " , customerId = " + customerId + " , partno = " + marketingPartNo);
                    
                    if((salesOrderId != null) && (salesOrderId.trim().length() != 0)) {
                            ordersList = SalesDAO.loadOnlineOrdersWithPurchaseOrder(salesOrderId,customerId,true);                                                            
                    } else {
                            ordersList = SalesDAO.loadAllOnlineOrders(customerId,marketingPartNo, true);
                            request.setAttribute("salesList",SalesDAO.loadViewOrdersWithoutDates(customerId+"",marketingPartNo,1,""));
                    }
                        
                     //Show the pendingList only when it searched
                     request.setAttribute("showList", "TRUE");
                }
                
                 if((action != null) && (ordersList.size() == 0))
                      errors.put( ERROR_SALES_ORDER_NOT_FOUND, null );
                        
                
                        
               // System.out.println("List of Order size is = " + ordersList.size());
                // Report any errors we have discovered back to the original form
                 if(!errors.isEmpty()) 
                            request.setAttribute(SessionKeys.MESSAGES, errors);
                    
                request.setAttribute("customerList",CustomerDAO.getAllCustomerNames());
                request.setAttribute("ordersList", ordersList);
                                                
                //Please look at this return later(Where do we need this retrurned) ????
                if((action != null) && (action.equals("CustomerOrders")))
                    return (mapping.findForward("customer"));
                else
                    return (mapping.findForward("success"));
                
    }

}
